package com.ccc.boot.monitor.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ccc.boot.entity.SysLog;
import com.ccc.boot.monitor.mapper.SysLogMapper;
import com.ccc.boot.monitor.service.SysLogService;
import com.ccc.boot.pojo.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author superC
 * @version 2.0.0
 * @date 2022/5/3
 */
@Service
public class SysLogServiceImpl implements SysLogService {

    @Autowired
    private SysLogMapper sysLogMapper;

    @Override
    public IPage<SysLog> pageList(Query query) {
        Page<SysLog> page = new Page<>(query.getCurrent(), query.getSize());
        LambdaQueryWrapper<SysLog> queryWrapper = new LambdaQueryWrapper<>();
        // 全局进行模糊查询
        query.forEach((column, value) -> {
            // 非空，进行模糊查询
            if ("condition".equals(column)) {
                queryWrapper.and(wrapper -> {
                    wrapper.like(SysLog::getUsername, value)
                            .or().like(SysLog::getMethod, value)
                            .or().like(SysLog::getPath, value);
                });
            } else if ("date".equals(column)) {
                queryWrapper.like(SysLog::getCreateTime, value);
            }
        });
        queryWrapper.orderByDesc(SysLog::getCreateTime);
        return sysLogMapper.selectPage(page, queryWrapper);
    }

}
